package com.whfc.base.manager.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.base.dao.AppHourlyWeatherMapper;
import com.whfc.base.dao.AppRealtimeWeatherMapper;
import com.whfc.base.dao.AppWeatherMapper;
import com.whfc.base.dao.AppWeatherTimeMapper;
import com.whfc.base.dto.AppWeatherDTO;
import com.whfc.base.entity.AppHourlyWeather;
import com.whfc.base.entity.AppRealtimeWeather;
import com.whfc.base.entity.AppWeatherTime;
import com.whfc.base.manager.WeatherMgr;
import com.whfc.common.enums.AppWeather;
import com.whfc.common.third.weather.WeatherApi;
import com.whfc.common.third.weather.WeatherDaily;
import com.whfc.common.third.weather.WeatherHourly;
import com.whfc.common.third.weather.WeatherInfo;
import com.whfc.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-10-13 10:34
 */
@Component
public class WeatherMgrImpl implements WeatherMgr {

    private Logger logger = LoggerFactory.getLogger(WeatherMgrImpl.class);

    @Autowired
    private WeatherApi weatherApi;

    @Autowired
    private AppWeatherMapper appWeatherMapper;

    @Autowired
    private AppWeatherTimeMapper appWeatherTimeMapper;

    @Autowired
    private AppRealtimeWeatherMapper appRealtimeWeatherMapper;

    @Autowired
    private AppHourlyWeatherMapper hourlyWeatherMapper;

    @Override
    public void refreshWeather(Integer deptId, Double lng, Double lat) {

        Date now = new Date();

        //获取天气数据
        WeatherInfo weather = weatherApi.getWeather(lng, lat);

        // 更新实时天气
        AppRealtimeWeather realtimeWeather = translate(weather);
        realtimeWeather.setDeptId(deptId);
        realtimeWeather.setTime(now);
        appRealtimeWeatherMapper.insertOrUpdate(realtimeWeather);

        // 刷新天气预报(按天)
        List<WeatherDaily> dailyWeatherList = weather.getDailyWeatherList();
        if (dailyWeatherList != null && dailyWeatherList.size() > 0) {
            List<AppWeatherDTO> weatherDTOList = dailyWeatherList.stream()
                    .map(this::translate)
                    .collect(Collectors.toList());
            for (AppWeatherDTO appWeatherDTO : weatherDTOList) {
                appWeatherDTO.setDeptId(deptId);
                appWeatherDTO.setUpdateTime(now);
            }
            appWeatherMapper.batchInsertOrUpdate(weatherDTOList);
        }

        // 刷新天气预报(按小时)
        List<WeatherHourly> hourlyWeatherList = weather.getHourlyWeatherList();
        if (hourlyWeatherList != null && hourlyWeatherList.size() > 0) {
            List<AppHourlyWeather> weatherList = hourlyWeatherList.stream()
                    .map(this::translate)
                    .collect(Collectors.toList());
            for (AppHourlyWeather hourlyWeather : weatherList) {
                hourlyWeather.setDeptId(deptId);
            }
            hourlyWeatherMapper.insertOrUpdate(weatherList);
        }

        //更新天气刷新时间
        AppWeatherTime weatherTime = new AppWeatherTime();
        weatherTime.setDeptId(deptId);
        weatherTime.setForecastWeatherTime(now);
        weatherTime.setRealtimeWeatherTime(now);
        appWeatherTimeMapper.insertOrUpdate(weatherTime);
    }
    public AppRealtimeWeather realtimeWeather( Double lng, Double lat) {

        Date now = new Date();

        //获取天气数据
        WeatherInfo weather = weatherApi.getWeatherRealtime(lng, lat);

        // 更新实时天气
        AppRealtimeWeather realtimeWeather = translate(weather);
        realtimeWeather.setTime(now);
        return realtimeWeather;

    }

    private AppRealtimeWeather translate(WeatherInfo weather) {
        AppWeather appWeather = AppWeather.parseValue(weather.getWeather());
        Double temperature = weather.getTemperature();
        String description = weather.getDescription();
        Double windSpeed = weather.getWindSpeed();
        List<Double> precipitationList = weather.getPrecipitationList();

        AppRealtimeWeather realtimeWeather = new AppRealtimeWeather();
        realtimeWeather.setTemperature(temperature);
        realtimeWeather.setDescription(description);
        realtimeWeather.setWeather(appWeather.getDesc());
        realtimeWeather.setWeatherPic(appWeather.getIcon());
        realtimeWeather.setPrecipitation(JSON.toJSONString(precipitationList));
        realtimeWeather.setWindSpeed(windSpeed);
        return realtimeWeather;
    }

    private AppWeatherDTO translate(WeatherDaily weatherDaily) {
        String date = weatherDaily.getDate();
        AppWeather appWeather = AppWeather.parseValue(weatherDaily.getWeather());
        Double max = weatherDaily.getMax();
        Double min = weatherDaily.getMin();
        AppWeatherDTO appWeatherDTO = new AppWeatherDTO();
        appWeatherDTO.setDate(DateUtil.parseDate(date, "yyyy-MM-dd"));
        appWeatherDTO.setWeather(appWeather.getDesc());
        appWeatherDTO.setWeatherPic(appWeather.getIcon());
        appWeatherDTO.setMaxTemperature(max);
        appWeatherDTO.setMinTemperature(min);
        appWeatherDTO.setPrecipitation(weatherDaily.getPrecipitation());
        appWeatherDTO.setWindPower(weatherDaily.getWindPower());
        appWeatherDTO.setWindDirection(weatherDaily.getWindDirection());
        return appWeatherDTO;
    }

    private AppHourlyWeather translate(WeatherHourly weatherHourly) {

        Double hourlyTemperature = weatherHourly.getTemperature();
        String time = weatherHourly.getTime();
        String weather = weatherHourly.getWeather();
        AppWeather appWeather = AppWeather.parseValue(weather);
        Double hourlyWindSpeed = weatherHourly.getWindSpeed();
        AppHourlyWeather hourlyWeather = new AppHourlyWeather();
        hourlyWeather.setTemperature(hourlyTemperature);
        hourlyWeather.setTime(DateUtil.parseDate(time, "yyyy-MM-dd'T'HH"));
        hourlyWeather.setWeather(weather);
        hourlyWeather.setWeatherPic(appWeather.getIcon());
        hourlyWeather.setWindSpeed(hourlyWindSpeed);
        return hourlyWeather;
    }

}
